<?php
class Commqueue extends CI_Controller
{
    public function commnotify()
    {
        if (!is_cli()) die('request forbidden');
        set_time_limit(0);
        ini_set('default_socket_timeout',-1);
        try{
            $this->load->model('redis_model');
            $this->load->database();
            $redis = $this->redis_model->getRedisInstance();
            while(true){
                $res = $redis->blPop('notifydeliver',0);
                $res = igbinary_unserialize($res[1]);
                switch ($res['type']){
                    case 'addq':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $sub = $this->db->select('name')->from('comm_subject')->where('id=',$res['data']['subid'])->get()->row_array();
                        $question = $this->db->select('question')->from('comm_question')->where('id=',$res['data']['qid'])->get()->row_array();
                        $notify = array();
                        $notify['msg']=$question['question'];
                        $notify['title'] = '用户'.$user['userName'].'在您关注的话题"'.$sub['name'].'"下发表了';
                        $notify['link'] = 'question.html?qid='.$res['data']['qid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $follows = $redis->sMembers('subject:'.$res['data']['subid'].'.follows');
                        $redis->multi();
                        foreach ($follows as $uid){
                            $redis->zAdd('user:'.$uid.'.notify.unread',time(),$msgid);
                        }
                        $redis->set('notifymsg:'.$msgid,$notify);
                        $redis->exec();
                        break;
                    case 'fq':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $question = $this->db->select('uid,question')->from('comm_question')->where('id=',$res['data']['qid'])->get()->row_array();
                        $notify = array();
                        $notify['title']='用户'.$user['userName'].'关注了您的问题';
                        $notify['msg'] = $question['question'];
                        $notify['link'] = 'question.html?qid='.$res['data']['qid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('user:'.$question['uid'].'.notify.unread',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                        break;
                    case 'addans':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $question = $this->db->select('uid,question')->from('comm_question')->where('id=',$res['data']['qid'])->get()->row_array();
                        $notify1 = array();
                        $notify1['title'] = '用户'.$user['userName'].'回答了您的问题';
                        $notify1['msg'] = $question['question'];
                        $notify1['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify1['external'] = 0;
                        $notify1['created'] = time();
                        $notify1 = igbinary_serialize($notify1);
                        $msgid1 = md5(uniqid(md5(microtime(true)),true));

                        $notify2 = array();
                        $notify2['title'] = '用户'.$user['userName'].'回答了您关注的';
                        $notify2['msg'] = $question['question'];
                        $notify2['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify2['external'] = 0;
                        $notify2['created'] = time();
                        $notify2 = igbinary_serialize($notify2);
                        $msgid2 = md5(uniqid(md5(microtime(true)),true));

                        $follows = $redis->sMembers('question:'.$res['data']['qid'].'.follows');
                        $redis->multi();
                        $redis->zAdd('user:'.$question['uid'].'.notify.unread',time(),$msgid1);
                        $redis->set('notifymsg:'.$msgid1,$notify1);
                        foreach ($follows as $uid){
                            $redis->zAdd('user:'.$uid.'.notify.unread',time(),$msgid2);
                        }
                        $redis->set('notifymsg:'.$msgid2,$notify2);
                        $redis->exec();
                        break;
                    case 'addcom':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $ans = $this->db->select('uid')->from('comm_answer')->where('id=',$res['data']['aid'])->get()->row_array();
                        $notify1 = array();
                        $notify1['title'] = '用户'.$user['userName'].'评论了您的观点';
                        $notify1['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify1['external'] = 0;
                        $notify1['created'] = time();
                        $notify1 = igbinary_serialize($notify1);
                        $msgid1 = md5(uniqid(md5(microtime(true)),true));

                        $notify2 = array();
                        $notify2['title'] = '用户'.$user['userName'].'评论了您收藏的观点';
                        $notify2['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify2['external'] = 0;
                        $notify2['created'] = time();
                        $notify2 = igbinary_serialize($notify2);
                        $msgid2 = md5(uniqid(md5(microtime(true)),true));

                        $collects = $redis->sMembers('answer:'.$res['data']['aid'].'.collects');
                        $redis->multi();
                        $redis->zAdd('user:'.$ans['uid'].'.notify.unread',time(),$msgid1);
                        $redis->set('notifymsg:'.$msgid1,$notify1);
                        foreach ($collects as $uid){
                            $redis->zAdd('user:'.$uid.'.notify.unread',time(),$msgid2);
                        }
                        $redis->set('notifymsg:'.$msgid2,$notify2);
                        $redis->exec();
                        break;
                    case 'agrans':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $ans = $this->db->select('uid')->from('comm_answer')->where('id=',$res['data']['aid'])->get()->row_array();
                        $notify = array();
                        $notify['title'] = '用户'.$user['userName'].'赞同了您的观点';
                        $notify['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('user:'.$ans['uid'].'.notify.unread',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                        break;
                    case 'collans':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $ans = $this->db->select('uid')->from('comm_answer')->where('id=',$res['data']['aid'])->get()->row_array();
                        $notify = array();
                        $notify['title'] = '用户'.$user['userName'].'收藏了您的观点';
                        $notify['link'] = 'answer.html?aid='.$res['data']['aid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('user:'.$ans['uid'].'.notify.unread',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                        break;
                    case 'cbeans':
                        $this->db->reconnect();
                        $user = $this->db->select('userName')->from('mod_user')->where('uid=',$res['data']['uid'])->get()->row_array();
                        $question = $this->db->select('uid,question')->from('comm_question')->where('id=',$res['data']['qid'])->get()->row_array();
                        $notify = array();
                        $notify['title']='用户'.$user['userName'].'对您的问题打赏了'.$res['data']['num'].'能量豆';
                        $notify['msg'] = $question['question'];
                        $notify['link'] = 'question.html?qid='.$res['data']['qid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('user:'.$question['uid'].'.notify.unread',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                        break;
                    case 'resq':
                        $this->db->reconnect();
                        $question = $this->db->select('question')->from('comm_question')->where('id=',$res['data']['qid'])->get()->row_array();
                        $notify = array();
                        $notify['title']='沈奕斐教授回答了女神榜问题';
                        $notify['msg'] = $question['question'];
                        $notify['link'] = 'question.html?qid='.$res['data']['qid'];
                        $notify['external'] = 0;
                        $notify['created'] = time();
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('sysnotify',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                        break;
                    case 'sys':
                        $notify = array();
                        $notify['title']=$res['data']['title'];
                        $notify['msg'] = $res['data']['msg'];
                        $notify['link'] = $res['data']['link'];
                        $notify['external'] = $res['data']['external'];
                        $notify['created'] = $res['data']['created'];
                        $notify = igbinary_serialize($notify);
                        $msgid = md5(uniqid(md5(microtime(true)),true));

                        $redis->zAdd('sysnotify',time(),$msgid);
                        $redis->set('notifymsg:'.$msgid,$notify);
                }
            }
        }catch (Exception $e){
            $err['type'] = 'commqueue';
            $err['time'] = date('Y-m-d H:i:s',time());
            $err['data'] = 'notifydeliver error : '.$e->getMessage();
            $err = igbinary_serialize($err);
            $this->redis_model->rpush('errlist',$err);
            $this->db->close();
            exit();
        }
    }

    public function addUserNotify()
    {
        $uid=$this->input->get('uid');
        $title=$this->input->get('title');
        $msg=$this->input->get('msg');
        $link=$this->input->get('link');
        $external=$this->input->get('external');
        if (!isset($uid)||!is_numeric($uid)
            ||!isset($title)||empty($title)
            ||!isset($msg)||empty($msg)
            ||!isset($link)||empty($link)
            ||!isset($external)||!is_numeric($external)
        ) die('request forbidden');

        $notify = array();
        $notify['title']=$title;
        $notify['msg'] = $msg;
        $notify['link'] = $link;
        $notify['external'] = (int)$external;
        $notify['created'] = time();
        $notify = igbinary_serialize($notify);
        $msgid = md5(uniqid(md5(microtime(true)),true));

        $this->load->model('redis_model');
        $redis = $this->redis_model->getRedisInstance();
        $redis->zAdd('user:'.$uid.'.notify.unread',time(),$msgid);
        $redis->set('notifymsg:'.$msgid,$notify);
        echo 'msg '.$msgid.' to user '.$uid.' complete!'.PHP_EOL;
    }

}